rm(list = ls())

setwd("/Users/Joan/Google Drive/Article Twitter Streams and Removal Rates/Data")
load("complete_data205k.RData")
library(psych)
library(plyr)
library(arm)
library(lme4)
library(Hmisc)

ret1 <- glmer(removed ~ lret.yes + (1 + lret.yes | .id), family=binomial (link="logit"), data=all.tweets) 
summary(ret1)
ret2 <- glmer(removed2 ~ lret.yes + (1 +  lret.yes | .id), family=binomial (link="logit"), data=all.tweets)
summary(ret2)
isbw1 <- glmer(removed2 ~ as.factor(is.bw) + (1 + as.factor(is.bw) | .id), family=binomial (link="logit"), data=all.tweets)
summary(isbw1)
isbw2 <- glmer(removed ~ as.factor(is.bw) + (1 + as.factor(is.bw) | .id), family=binomial (link="logit"), data=all.tweets)
summary(isbw2)
isfw1 <- glmer(removed2 ~ as.factor(is.fw) + (1 + as.factor(is.fw) | .id), family=binomial (link="logit"), data=all.tweets)
summary(isfw1)
isfw2 <- glmer(removed ~ as.factor(is.fw) + (1 + as.factor(is.fw) | .id), family=binomial (link="logit"), data=all.tweets)
summary(isfw2)
lang <- glmer(removed ~ as.factor(is.bw) + (1 + as.factor(is.bw) | lang.cat), family=binomial (link="logit"), data=all.tweets)
summary(lang)

setwd("/Users/Joan/Google Drive/Article Twitter Streams and Removal Rates/TeX")

## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## 
## RANDOM SLOPE FOR LRETWEET, fig 6
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##


tiff("Fig6.tif", width=3500, height=2250, pointsize=12, res=300)

par(mfrow=c(1,2), oma = c(1,0.1,2,1), mar=c(4,3,4,2)) #B, L, U, R

## RANDOM SLOPE FOR LRET with REMOVED 1
plot(x=NULL, y=NULL, xlim=c(0,8), ylim=c(0,0.32), type="n", main="(a) Removed before 15 minutes", 
     xlab="Retweet Count (Log)", ylab="Predicted Probability", xaxt = "n", yaxt = "n", line=2.2, cex.main=1, cex.lab=0.9)
for(i in 1:length(coef(ret1)[[1]][,1])){
  coefs <- coef(ret1)[[1]][i,]
  #a <- 
  curve(invlogit(as.double(coefs[1]) + as.double(coefs[2])*x), 
        from=0, to=8, col=i, ylim=c(0,0.32), add=TRUE)
  # if(a$y[length(a$y)]>0.075){
  #   text(4,0.2-1/i, paste0(rownames(coef(ret1)[[1]][i,]), i), col=i)
  #   
  # } else {
  #   next
  # }
  #Sys.sleep(1)
}
abline(h=0, col="red", lty=2)
axis(2, at=c(seq(0,0.3,0.05)), las=3, cex.axis=0.9, main="")
axis(1, at=c(seq(0,8,1)), cex.axis=0.9)
#lines(density(all.tweets$lret.yes, na.rm=T), lty=2)
legend(6.3,0.33, c("Perrito", "Gato", "Fifa", "Syria"), cex=0.7, lty = c(1,1), 
       col=c("23", "10", "9", "28"), bty='n')

## RANDOM SLOPE FOR LRET with REMOVED 2
plot(x=NULL, y=NULL, xlim=c(0,8), ylim=c(0,0.7), type="n", main="(b) Removed before 7 days", 
     xlab="Retweet Count (Log)", ylab="Predicted Probability", xaxt = "n", yaxt = "n", line=2.2, cex.main=1, cex.lab=0.9)
for(i in 1:length(coef(ret2)[[1]][,1])){
  coefs <- coef(ret2)[[1]][i,]
  a <- curve(invlogit(as.double(coefs[1]) + as.double(coefs[2])*x), 
        from=0, to=8, col=i, ylim=c(0,1), add=TRUE)
  # if(a$y[1]>0.1){
  #   text(4,0.6-3/i, paste0(rownames(coef(ret1)[[1]][i,]), i), col=i)
  # 
  # } else {
  #   next
  # }
  #Sys.sleep(1)
}
abline(h=0, col="red")
axis(2, at=c(seq(0,0.7,0.1)), las=3, cex.axis=0.9)
axis(1, at=c(seq(0,8,1)), cex.axis=0.9)
par(new=T)
#plot(density(all.tweets$lret.yes, na.rm=T), lty=2, xaxt = "n", yaxt = "n", 
#    xlab="", ylab="", main="")
#axis(side = 4, las=1, cex.axis=0.9)
text(2,0.7, substitute(paste(italic("Negative Slope:"))), cex=0.7)
text(7.2,0.7, substitute(paste(italic("Positive Slope:"))), cex=0.7)
legend(6.3,0.7, c("Perrito", "Fifa", "Gato", "Two", "Syria", "Lasagna"), cex=0.7, lty = c(1,1), 
       col=c("23", "9", "10", "33", "28", "15"), bty='n')
legend(1,0.7, c("Islamic State (Ar.)", "Daesh (Ar.)"), cex=0.7, lty = c(1,1), 
       col=c("13", "36"), bty='n')


mtext("Predicted Probabilities of Removal by Retweet Count",outer = TRUE, font=3, cex = 1.3)
mtext("Note: Only groups displaying meaningful change are shown in the legend. Similar colors are ordered by probability at highest values of retweet count. (Ar.) = Arabic.", outer = TRUE, side=1, adj=0,font=3, cex = 0.8)

dev.off()




## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## 
## IS BACKWARD AND FORWARD, fig 7
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##



tiff("Fig7.tif", width=2300, height=2300, pointsize=12, res=300)

par(mfrow=c(2,2), oma = c(1.1,1,2,0), mar=c(3,2,3,1)) #B, L, U, R

## RANDOM SLOPE FOR IS BACKWARD with REMOVED 1
plot(x=NULL, y=NULL, xlim=c(0,1), ylim=c(0,0.6), type="n", main="Removed before 15 minutes", 
     xlab="Is in Backward Stream", ylab="", xaxt = "n",yaxt = "n", line=1, cex.main=0.9,
     cex.lab=0.8)
for(i in 1:length(coef(isbw2)[[1]][,1])){
  coefs <- coef(isbw2)[[1]][i,]
  segments(0, invlogit(as.double(coefs[1])),
           1,invlogit(as.double(coefs[1]) + as.double(coefs[2])*1), 
           col=i, lty=2)
  points(0, invlogit(as.double(coefs[1])), col=i,  pch=19)
  points(1, invlogit(as.double(coefs[1]) + as.double(coefs[2])*1), col=i,  pch=19)
}
abline(h=0, col="red")
axis(1, at=c(0,1), labels=c("NO", "YES"), cex.axis=0.8)
axis(2, at=c(seq(0.1,0.6,0.1)), las=1, cex.axis=0.8)
legend(0.7,0.6, c("Daesh (Ar.)"), cex=0.7, lty = 1, col=c("36"),bty='n')

## RANDOM SLOPE FOR IS BACKWARD with REMOVED 2
plot(x=NULL, y=NULL, xlim=c(0,1), ylim=c(0,0.7), type="n", main="Removed before 7 days", 
     xlab="Is in Backward Stream", ylab="", xaxt = "n", yaxt = "n", line=1, cex.main=0.9,
     cex.lab=0.8)
for(i in 1:length(coef(isbw1)[[1]][,1])){
  coefs <- coef(isbw1)[[1]][i,]
  segments(0, invlogit(as.double(coefs[1])),
           1,invlogit(as.double(coefs[1]) + as.double(coefs[2])*1), 
           col=i, lty=2)
  points(0, invlogit(as.double(coefs[1])), col=i,  pch=19)
  points(1, invlogit(as.double(coefs[1]) + as.double(coefs[2])*1), col=i,  pch=19)
}
abline(h=0, col="red")
axis(1, at=c(0,1), labels=c("NO", "YES"), cex.axis=0.8)
axis(2, at=c(seq(0.1,1,0.1)), las=1, cex.axis=0.8)
legend(0.65,0.7, c("Islamic State (Ar.)", "Daesh (Ar.)", "Koala"), cex=0.7, lty = 1, col=c("13", "36", "14"),bty='n')

## RANDOM SLOPE FOR IS FORWARD with REMOVED 1
plot(x=NULL, y=NULL, xlim=c(0,1), ylim=c(0,0.2), type="n", main="Removed before 15 minutes", 
     xlab="Is in Forward Stream", ylab="", xaxt = "n", yaxt = "n", line=1, cex.main=0.9,
     cex.lab=0.8)
for(i in 1:length(coef(isfw2)[[1]][,1])){
  coefs <- coef(isfw2)[[1]][i,]
  segments(0, invlogit(as.double(coefs[1])),
           1,invlogit(as.double(coefs[1]) + as.double(coefs[2])*1), 
           col=i, lty=2)
  points(0, invlogit(as.double(coefs[1])), col=i,  pch=19)
  points(1, invlogit(as.double(coefs[1]) + as.double(coefs[2])*1), col=i,  pch=19)
}
abline(h=0, col="red")
axis(1, at=c(0,1), labels=c("NO", "YES"), cex.axis=0.8)
axis(2, at=c(seq(0,0.2,0.05)), las=1, cex.axis=0.8)
legend(0.65,0.2, c("Islamic State (Ar.)", "Daesh (Ar.)", "Perrito"), cex=0.7, lty = 1, col=c("13", "36", "23"),bty='n')


## RANDOM SLOPE FOR IS FORWARD with REMOVED 2
plot(x=NULL, y=NULL, xlim=c(0,1), ylim=c(0,0.7), type="n", main="Removed before 7 days", 
     xlab="Is in Forward Stream", ylab="", xaxt = "n", yaxt = "n", line=1, cex.main=0.9,
     cex.lab=0.8)
for(i in 1:length(coef(isfw1)[[1]][,1])){
  coefs <- coef(isfw1)[[1]][i,]
  segments(0, invlogit(as.double(coefs[1])),
           1,invlogit(as.double(coefs[1]) + as.double(coefs[2])*1), 
           col=i, lty=2)
  points(0, invlogit(as.double(coefs[1])), col=i,  pch=19)
  points(1, invlogit(as.double(coefs[1]) + as.double(coefs[2])*1), col=i,  pch=19)
}
abline(h=0, col="red")
axis(1, at=c(0,1), labels=c("NO", "YES"), cex.axis=0.8)
axis(2, at=c(seq(0,0.7,0.1)), las=1, cex.axis=0.8)
legend(0,0.7, c("Islamic State (Ar.)", "Daesh (Ar.)"), cex=0.7, lty = 1, col=c("13", "36"),bty='n')

mtext("Predicted Probabilities of Removal by Presence in API",outer = TRUE, font=3, cex = 1)
mtext("Note: Only groups displaying meaningful change are shown in the legend. (Ar.) = Arabic.", outer = TRUE, side=1, adj=0,font=3, cex = 0.8)

dev.off()




